public class BankAccount {
    //私有属性：账户余额
    private double balance;
    //构造方法
    public BankAccount(double initialBalance){
    this.balance=initialBalance;
}

    @Override
    public String toString() {
        return "BankAccount{" +
                "balance=" + balance +
                '}';
    }

    //公共方法：存款
    public void deposit(double amount) {
        balance += amount;
        System.out.println("Deposited:$"+amount);
        System.out.println("New balance:$"+balance);
    }
    //公共方法：取款

    public void setBalance(double balance) {
        this.balance = balance;
    }

    public void withdraw(double amount) {
       balance = amount;
        System.out.println("Withdrew:$"+amount);
        System.out.println("New balance:$"+balance);
    }
    //公共方法：获取账户余额

    public double getBalance() {
        return balance;
    }
    //私有方法：这个方法不应该被外部访问，因此是私有的
    private void printBalance(){
       System.out.println("Balance (should not be directly accessible): $" + balance);
    }
    //主方法用于测试
    public static void main(String[] args) {
        // 创建一个银行账户对象
        BankAccount account = new BankAccount(1000.0);
        // 存款
        account.deposit(500.0);
        // 取款
        account.withdraw(200.0);
        // 获取账户余额
        System.out.println("Account balance: $" + account.getBalance());
        // 尝试调用私有方法（这将导致编译错误）
       // account.printBalance(); // 编译错误：The method printBalance() from the  type BankAccount is not visible
    }
}

